package com.soulless.reflect.demo;

/**
 * <p>
 * 传统工厂模式的优化
 * </p>
 *
 * @author zenglei
 * @date 2022/04/20/ 19:05
 */
public class Demo2 {

    public static void main(String[] args) {
        Fruit2 f = Factory2.getInstance("com.soulless.reflect.demo.Orange");
        f.eat();
    }

}

interface Fruit2 {
    public void eat();
}

class Apple2 implements Fruit2 {
    public void eat() {
        System.out.println("吃苹果。");
    }
}

class Orange implements Fruit2 {
    public void eat() {
        System.out.println("吃橘子。");
    }
}

class Factory2 {
    public static Fruit2 getInstance(String className) {
        Fruit2 f = null;
        try {
            f = (Fruit2) Class.forName(className).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return f;
    }
}
